<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Channel;

/**
 * ChannelSearch represents the model behind the search form about `app\models\Channel`.
 */
class ChannelSearch extends Channel
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cid', 'create_time', 'manager_id', 'update_time', 'fid', 'display', 'type', 'seq'], 'integer'],
            [['cname', 'list_template', 'show_template'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Channel::find()->orderBy("create_time desc");

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'cid' => $this->cid,
            'create_time' => $this->create_time,
            'manager_id' => $this->manager_id,
            'update_time' => $this->update_time,
            'fid' => $this->fid,
            'display' => $this->display,
            'type' => $this->type,
            'seq' => $this->seq,
        ]);

        $query->andFilterWhere(['like', 'cname', $this->cname])
            ->andFilterWhere(['like', 'list_template', $this->list_template])
            ->andFilterWhere(['like', 'show_template', $this->show_template]);

        return $dataProvider;
    }
}
